// MODEL ANSWER - CONFIDENTIAL
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;  // for ChangListener
import java.awt.geom.*;
import java.text.*;  // for NumberFormat
import java.beans.*;  // for PropertyChangeEvent etc
/**
 HIT3687 Assignment (Viewfinder Stage) : AbstractMapPanel is the superclass 'view' for
 the system -- it displays the coastline data only.  
 <BR>Stage 4: introduced.
 @version 4.1 11/10/02
 @author Dr Rob Allen, Swinburne University of Technology
 */
public class SimpleMapPanel extends JPanel
{
    static final boolean debugging = false;
    
    protected Region data;
    protected AffineTransform af, inverse;
    protected double scale = 10.0;  
    protected int zoomFactor = 1;
    protected Rectangle2D.Double outerRect;
    protected Rectangle2D.Double innerRect;  
    protected DecimalFormat df = new DecimalFormat("##0.000");
    protected Stroke thinStroke;
    
   
    public SimpleMapPanel()
    {
        setBackground(Color.white);
        setForeground(Color.black);
        setDoubleBuffered(true);
    }
   
    protected String format(Point2D lonlat)
    {
        return "(" + df.format(lonlat.getX())
             + ", " + df.format(lonlat.getY()) + ")";
    }

    protected String format(Rectangle2D r)
    {
        return "[" + df.format(r.getX())
             + ", " + df.format(r.getY())
             + ", " + df.format(r.getWidth())
             + ", " + df.format(r.getHeight()) + "]";
    }
    
    /**
     Draw the outlines of all regions, scaled to fit; 
     set af, scale, inverse.
    */
    public void paintComponent(Graphics g)
    {
    	super.paintComponent(g);
        if (data == null) 
        {
        	g.drawString("No data loaded yet.", 10, 10);
            return;
        }
        Graphics2D g2 = (Graphics2D)g;
        Dimension d = getSize();
      
        double xscale = d.getWidth()/outerRect.getWidth();
        double yscale = d.getHeight()/outerRect.getHeight();
        scale = Math.min(xscale, yscale);
        af = new AffineTransform();  // identity
        af.translate(0,+d.getHeight());  // done last
        af.scale(scale, -scale);  // invert
        af.translate(-outerRect.getX(), -outerRect.getY()); //first
      
        g2.transform(af);
        thinStroke = new BasicStroke((float)(1.25/scale));
        g2.setStroke(thinStroke);
        
        for (State r : data.getState().values()){
			g2.setColor(r.getColour());
			g2.draw(r.getDrawPath());
		
		}
        
        // save inverse transform for use in mouse following
        try {
            inverse = af.createInverse();
        }
        catch (NoninvertibleTransformException e) 
        { 
            inverse = null;
            return;
        }
    }
    
    public void setData(Region data)
    {
        this.data = data;
        outerRect = data.getBounds();
        //innerRect = outerRect.getBounds2D();  // clone, n.y.used
        zoomFactor = 1;
    }
        
}